package com.szewec.ps.ui.road.facilities.intersection.task;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.szewec.ps.R;
import com.szewec.ps.base.BaseActivity;
import com.szewec.ps.base.Constants;
import com.szewec.ps.databinding.ActivityRoadHierarchyAddBinding;
import com.szewec.ps.model.PresentValue;
import com.szewec.ps.myview.toast.bamtoast.BamToast;
import com.szewec.ps.ui.road.facilities.intersection.task.enums.RoadEnums;
import com.szewec.ps.ui.road.facilities.intersection.task.model.RoadHierarchy;
import com.szewec.ps.util.RollUtil;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.TextViewUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;

public class RoadHierarchyAddActivity extends BaseActivity {

    private ActivityRoadHierarchyAddBinding binding;

    private RoadHierarchy roadHierarchy = new RoadHierarchy();

    private List<String> roadLevelList = new ArrayList<>();
    private List<Integer> trafficRoadNumList = new ArrayList<>();
    int junctionType;
    private String flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_road_hierarchy_add);
        ButterKnife.bind(this);

        Bundle presentValue = getIntent().getExtras();
        PresentValue<Map<String, Object>> value = (PresentValue<Map<String, Object>>) presentValue.getSerializable(Constants.EXTRA_KEY.ROAD_LEVEL);
        flag = value.getFlag();
        junctionType = (int) value.getT().get("junctionType");
        if (Constants.PRESENT_VALUE_FLAG.ADD.equals(flag)) {
            if (junctionType == (RoadEnums.JunctionType.INTERCHANGE.getKey())) {
                binding.roadOrder.setVisibility(View.VISIBLE);
                Integer trafficRoadNum = (Integer) value.getT().get("trafficRoadNum");
                for (int i = 1; i <= trafficRoadNum; i++) {
                    trafficRoadNumList.add(i);
                }
            } else {
                binding.roadOrder.setVisibility(View.GONE);
            }
        } else if (Constants.PRESENT_VALUE_FLAG.EDIT.equals(flag)) {
            roadHierarchy = (RoadHierarchy) value.getT().get("roadLevel");
            binding.roadName.setValue(roadHierarchy.getRoadName());
            binding.roadLevel.setValue(roadHierarchy.getRoadLevel());
            if (junctionType == (RoadEnums.JunctionType.INTERCHANGE.getKey())) {
                binding.roadOrder.setVisibility(View.VISIBLE);
                Integer trafficRoadNum = (Integer) value.getT().get("trafficRoadNum");
                for (int i = 1; i <= trafficRoadNum; i++) {
                    trafficRoadNumList.add(i);
                }
                binding.roadOrder.setValue(roadHierarchy.getRoadOrder());
            } else {
                binding.roadOrder.setVisibility(View.GONE);
            }
        }

        initTitleBar();
        initData();
        initInputView();
    }

    private void initTitleBar() {
        binding.titleBar.setLeftLayoutVisibility(View.VISIBLE);
        binding.titleBar.setLeftLayoutClickListener(this::finish);
        binding.titleBar.setRightLayoutVisibility(View.VISIBLE);
        binding.titleBar.setRightTextVisibility(View.VISIBLE);

        binding.titleBar.setRightTextClickListener(v -> {
            String roadName = binding.roadName.getStringValue();
            String level = binding.roadLevel.getStringValue();
            String roadHierarchy = binding.roadOrder.getStringValue();
            if (StringUtils.isEmpty(roadName) || StringUtils.isEmpty(level)) {
                BamToast.showText(this, R.string.requires_text_filed_check, false);
            } else if (junctionType == (RoadEnums.JunctionType.INTERCHANGE.getKey()) && StringUtils.isEmpty(roadHierarchy)) {
                BamToast.showText(this, R.string.requires_text_filed_check, false);
            } else {
                this.roadHierarchy.setRoadName(roadName);
                this.roadHierarchy.setRoadLevel(level);
                this.roadHierarchy.setRoadOrder(roadHierarchy);
                Intent intent = new Intent();
                PresentValue<RoadHierarchy> presentValue = new PresentValue<>(flag, this.roadHierarchy);
                intent.putExtra(Constants.EXTRA_KEY.ROAD_LEVEL, presentValue);
                setResult(Constants.RESULT_CODE.NEED_REFRESH, intent);
                finish();
            }
        });
    }

    private void initData() {
        roadLevelList = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.road_level)));
    }

    private void initInputView() {
        binding.roadLevel.setOnClickListener(v -> {
            TextViewUtil.closeKeyboard(this);
            RollUtil.alertBottomWheelOption(this, roadLevelList, binding.roadLevel.getSelectTextView(), (view, index) ->
                    binding.roadLevel.setValue(roadLevelList.get(index)));
        });
        binding.roadOrder.setOnClickListener(v -> {
            TextViewUtil.closeKeyboard(this);
            RollUtil.alertBottomWheelOption(this, trafficRoadNumList, binding.roadOrder.getSelectTextView(), (view, index) ->
                    binding.roadOrder.setValue(trafficRoadNumList.get(index)));
        });
    }
}